package com.onlinestudy.controller;

import com.onlinestudy.domain.CourseMaterial;
import com.onlinestudy.dto.CourseMaterialDto;
import com.onlinestudy.service.CourseMaterialService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/materials")
@CrossOrigin(origins = "*", maxAge = 3600)
public class CourseMaterialController {

    private final CourseMaterialService materialService;

    public CourseMaterialController(CourseMaterialService materialService) {
        this.materialService = materialService;
    }

    @PostMapping
    public ResponseEntity<CourseMaterial> addMaterial(@RequestBody CourseMaterialDto materialDto) {
        CourseMaterial newMaterial = materialService.addMaterialToCourse(materialDto);
        return new ResponseEntity<>(newMaterial, HttpStatus.CREATED);
    }

    @PostMapping("/upload")
    public ResponseEntity<CourseMaterial> uploadMaterial(
            @RequestParam("file") MultipartFile file,
            @RequestParam("courseId") Long courseId,
            @RequestParam("title") String title,
            @RequestParam("materialType") String materialType,
            @RequestParam(value = "order", defaultValue = "0") int order) {
        
        CourseMaterial uploadedMaterial = materialService.uploadMaterial(file, courseId, title, materialType, order);
        return new ResponseEntity<>(uploadedMaterial, HttpStatus.CREATED);
    }

    @GetMapping("/course/{courseId}")
    public ResponseEntity<List<CourseMaterial>> getMaterialsByCourse(@PathVariable Long courseId) {
        List<CourseMaterial> materials = materialService.getMaterialsForCourse(courseId);
        return ResponseEntity.ok(materials);
    }

    @PutMapping("/{materialId}")
    public ResponseEntity<CourseMaterial> updateMaterial(@PathVariable Long materialId, @RequestBody CourseMaterialDto materialDto) {
        CourseMaterial updatedMaterial = materialService.updateMaterial(materialId, materialDto);
        return ResponseEntity.ok(updatedMaterial);
    }

    @DeleteMapping("/{materialId}")
    public ResponseEntity<Void> deleteMaterial(@PathVariable Long materialId) {
        materialService.deleteMaterial(materialId);
        return ResponseEntity.noContent().build();
    }
} 